import Vue from 'vue';
import store from '../store/index'
import VueRouter from 'vue-router';
Vue.use(VueRouter);

const routes = [
  // 根页面
  {
    path: "/",
    component: () => import( /* webpackChunkName: "root" */ "../views/Root.vue"),
    children: [{
        path: "",
        redirect: "/home",
      },
      // 首页
      {
        path: "home",
        name: "home",
        component: () =>
          import( /* webpackChunkName: "home" */ "../views/Home/Index.vue"),
        children: [{
            // 附近房源
            path: "/home/vicinity",
            name: "vicinity",
            component: () =>
              import(
                /* webpackChunkName: "vicinity" */
                "../views/Home/Operate/Vicinity.vue"
              ),
          },
          {
            // 收藏
            path: "/collect",
            name: "collect",
            component: () =>
              import(
                /* webpackChunkName: "collect" */
                "../views/Home/Operate/Collect.vue"
              ),
          },
          {
            // 浏览记录
            path: "/home/record",
            name: "record",
            component: () =>
              import(
                /* webpackChunkName: "record" */
                "../views/Home/Operate/Record.vue"
              ),
          },
          {
            //成为房东
            path: "/hasHandload",
            name: "hasHandload",
            component: () =>
              import("../views/Mine/com/myHomeInfo/hasHandload.vue"),
          },
          {
            // 酒店详情页
            path: "/home/detail/:id",
            name: 'detail',
            component: () => import( /* webpackChunkName: "detail" */ "../views/Home/Detail/Detail.vue"),
            children: [{
              // 生成订单
              path: "/home/detail/:id/createorder",
              name: "createorder",
              component: () => import( /* webpackChunkName: "createorder" */ "@/views/Home/Createorder/Createorder.vue"),
              children: [{
                // 支付页面
                path: "/home/detail/:id/createorder/payment",
                name: "payment",
                component: () =>
                  import( /* webpackChunkName: "payment" */ "@/views/Home/Payment/Index.vue"),
                  children: [
                    {
                      // 支付页面
                      path: "/home/detail/:id/createorder/payment/paymentdetail",
                      name: "paymentdetail",
                      component: () =>
                        import(
                          /* webpackChunkName: "paymentdetail" */ "@/views/Home/PayDetail/Index.vue"
                        ),
                    },
                  ],
              }, 
              
            ]
            }]
          }
        ],
      },
      // 发现
      {
        path: "spot",
        name: "spot",
        component: () =>
          import( /* webpackChunkName: "spot" */ "../views/Spot/Index.vue"),
      },
      // 聊天
      {
        path: "chat",
        name: "chat",
        component: () =>
          import( /* webpackChunkName: "chat" */ "../views/Chat/Index.vue"),
        children: [
          // 聊天页面
          {
            path: "chat-item",
            name: "chat-item",
            component: () =>
              import(
                /* webpackChunkName: "chat-item" */
                "../views/Chat/ChatItem/Index.vue"
              ),
          },
        ],
      },
      // 我的
      {
        path: "mine",
        name: "mine",
        component: () =>
          import( /* webpackChunkName: "mine" */ "../views/Mine/Index.vue"),
        children: [{
            path: "myorder", //我的订单
            name: "myorder",
            component: () => import("@/views/Mine/com/order/orderItem.vue"),
            children: [{
              // 订单详情
              path: "/mine/myorder/orderlist/:orderId",
              name: "orderlist",
              component: () => import("@/views/Mine/com/order/orderlist.vue"),
            }, ],
          },
          {
            //个人资料
            path: "userinfo",
            name: "userinfo",
            component: () => import("../views/Mine/com/userinfo/userInfo.vue"),
          },
          {
            //修改密码
            path: "modifyPsw",
            name: "modifyPsw",
            component: () =>
              import("../views/Mine/com/modifypsw/modifypsw.vue"),
          },
          {
            //我的房源信息
            path: "myhomeinfo",
            name: "myhomeinfo",
            component: () =>
              import("../views/Mine/com/myHomeInfo/myHomeInfo.vue"),
            children: [{
              //我的房源信息
              path: "checkhomeinfo",
              name: "checkhomeinfo",
              component: () =>
                import("../views/Mine/com/myHomeInfo/checkhomeinfo.vue"),
            }, ],
          },
          {
            //成为房东
            path: "hashandload",
            name: "hashandload",
            component: () =>
              import("../views/Mine/com/myHomeInfo/hasHandload.vue"),
          },
          {
            //我的钱包
            path: "mypack",
            name: "mypack",
            component: () => import("../views/Mine/com/mypack/myPack.vue"),
          },
          {
            //我的优惠券
            path: "mysale",
            name: "mysale",
            component: () => import("../views/Mine/com/mysale/mySale.vue"),
            children: [{
              //兑换中心
              path: "mysale/convert",
              name: "convert",
              component: () => import("../views/Mine/com/mysale/convert.vue"),
            }, ],
          },
          {
            //消息中心
            path: "msgcenter",
            name: "msgcenter",
            component: () =>
              import("../views/Mine/com/msgcenter/msgCenter.vue"),
          },
          {
            //意见反馈
            path: "useroption",
            name: "useroption",
            component: () =>
              import("../views/Mine/com/useroption/useroption.vue"),
          },
          {
            //关于
            path: "about",
            name: "about",
            component: () => import("../views/Mine/com/about/about.vue"),
          },
        ],
      },
    ],
  },
  // 登录
  {
    path: "/login",
    name: "login",
    component: () =>
      import( /* webpackChunkName: "auth" */ "../views/Login.vue"),
  },
  // 忘记密码
  {
    path: "/forgetpsw",
    name: "forgetpsw",
    component: () =>
      import( /*webpackChunkName:'auth' */ "../views/ForgetPsw.vue"),
  },
  // 注册
  {
    path: "/register",
    name: "register",
    component: () =>
      import( /* webpackChunkName: "auth" */ "../views/Register.vue"),
  },
  {
    path: "/404",
    component: () =>
      import( /* webpackChunkName: "root" */ "../views/NotFound.vue"),
  },
  // 匹配剩余的所有路由，需要放在最后面
  {
    path: "*",
    redirect: "/404",
  },
];

const router = new VueRouter({
  routes,
  mode: 'history',
});


router.beforeEach((to, from, next) => {
  const {
    name
  } = to;
  //直接进入登录/注册
  if (name === 'login' || name === 'register' || name === 'forgetpsw') {
    next();
  }
  //其他页面，需要判断用户是否登录了。
  else {
    if (store.state.user.isLogin) {
      // console.log(this.$store);
      next(); //登录了，直接进入
    } else {
      next("/login");
      // console.log(this.$store);

    }
  }
});

export default router;